package com.drug.mingkang.service.impl;


import com.drug.mingkang.service.ExperssageService;
import com.drug.mingkang.utils.KdniaoTrackQueryAPI;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * 物流管理
 * @author Mr.wang
 * @since 2020-08-11 12:00:12
 */
@Service
public class ExperssageServiceImpl extends  UserServiceImpl implements ExperssageService {
    /**
     * 根据快递单号追踪信息
     * @param cohr
     * @param ExpressNumber
     * @return
     */
    @Override
    public Map<String, Object> findExpressageInfo(String cohr, String ExpressNumber) {
        Map<String, Object> map = new HashMap<>();
        KdniaoTrackQueryAPI api = new KdniaoTrackQueryAPI();
        try {
            //第一个参数是快递公司简称（YD -- 韵达速递）
            //第二个参数是需要查询的快递单号
            String result = api.getOrderTracesByJson(cohr, ExpressNumber);
            JSONObject jsonObject = JSONObject.fromObject(result);
            if(jsonObject.containsKey("ShipperCode")){
                String ShipperCode = jsonObject.getString("ShipperCode");
                String LogisticCode = jsonObject.getString("LogisticCode");
                //轨迹
                JSONArray Traces = jsonObject.getJSONArray("Traces");
                System.out.println("快递名称"+ShipperCode);
                System.out.println("快递单号"+LogisticCode);
                int count = 1;
                for(int i = 0; i < Traces.size(); i++) {
                    JSONObject object = (JSONObject) Traces.get(i);
                    String AcceptTime = object.getString("AcceptTime");
                    String AcceptStation = object.getString("AcceptStation");
                    System.out.println("时间："+AcceptTime+"\t"+AcceptStation);
                    map.put("时间"+count,AcceptTime+"="+AcceptStation);
                    count++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.print("结束");
        }
        return map;
    }

    /**
     * 根据快递单号追踪信息
     * @param cohr
     * @param ExpressNumber
     * @return
     */
    @Override
    public Map<String, String> findExpressageInfoTraces(String cohr, String ExpressNumber) {
        Map<String, String> map = new HashMap<>();
        KdniaoTrackQueryAPI api = new KdniaoTrackQueryAPI();
        try {
            //第一个参数是快递公司简称（YD -- 韵达速递）
            //第二个参数是需要查询的快递单号
            String result = api.getOrderTracesByJson(cohr, ExpressNumber);
            JSONObject jsonObject = JSONObject.fromObject(result);
            if(jsonObject.containsKey("ShipperCode")){
                String ShipperCode = jsonObject.getString("ShipperCode");
                String LogisticCode = jsonObject.getString("LogisticCode");
                //轨迹
                JSONArray Traces = jsonObject.getJSONArray("Traces");
                System.out.println("快递名称"+ShipperCode);
                System.out.println("快递单号"+LogisticCode);
                int count = 1;
                for(int i = 0; i <Traces.size(); i++) {
                    JSONObject object = (JSONObject) Traces.get(i);
                    String AcceptTime = object.getString("AcceptTime");
                    String AcceptStation = object.getString("AcceptStation");
                    if(i==Traces.size()-1){
                        System.out.println(AcceptStation);
                        //是否退货成功
                        map.put(""+(count),AcceptStation);
                    }
                    String substring = AcceptStation.substring(AcceptStation.lastIndexOf("【") + 1, AcceptStation.lastIndexOf("】"));
                    map.put(""+count,substring);
                    count++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.print("结束");
        }
        return map;
    }
}